package com.example.test.主方法运行测试;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.example.test.bean.BasicArea;
import com.github.dadiyang.equator.Equator;
import com.github.dadiyang.equator.FieldInfo;
import com.github.dadiyang.equator.GetterBaseEquator;

import java.lang.reflect.Field;
import java.util.List;

public class CompareBean {

    /**
     * @Description 实体类比对,修改属性
     * @Author FL
     * @Date 14:17 2022/2/15
     * @Param [args]
     **/
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {

        BasicArea basicArea = new BasicArea();
        basicArea.setCode("123");
        basicArea.setColumn1("321");


        BasicArea basicArea1 = new BasicArea();
        basicArea1.setCode("1238");
        basicArea1.setColumn1("4321");

        Equator equator = new GetterBaseEquator();
        List<FieldInfo> diff = equator.getDiffFields(basicArea, basicArea1);

        if (CollectionUtil.isNotEmpty(diff)) {
            for (FieldInfo fieldInfo : diff) {
                if ("code".contains(fieldInfo.getFieldName())) {
                    Field field = basicArea.getClass().getDeclaredField(fieldInfo.getFieldName());
                    field.setAccessible(true);
                    field.set(basicArea,fieldInfo.getSecondVal());
                }
            }
        }

//        System.out.println(JSONUtil.toJsonStr(basicArea));
//
//        if (CollectionUtil.isNotEmpty(diff)) {
//            System.out.println("不一样");
//        }else {
//            System.out.println("一样");
//        }
    }
}
